package org.xukai.common;

import com.google.common.base.Objects;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Ordering;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;

/**
 * @author xukai
 * @desc
 * @date 2016-09-21-下午 1:27
 */
@XmlRootElement(name = "Student")
public class ComparableStudent implements Comparable<ComparableStudent> , Serializable {


    @XmlElement
    public String name;
    @XmlElement
    public int age;
    @XmlElement
    public int score;


    public ComparableStudent(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score=score;
    }

//    @Override
//    public int hashCode() {
//        return Objects.hashCode(name, age);
//    }


    @Override
    public boolean equals(Object obj) {
        if (obj instanceof ComparableStudent) {
            ComparableStudent that = (ComparableStudent) obj;
            return Objects.equal(name, that.name)
                    && Objects.equal(age, that.age)
                    && Objects.equal(score, that.score);
        }
        return false;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this)
                .addValue(name)
                .addValue(age)
                .addValue(score)
                .toString();
    }


    @Override
    public int compareTo(ComparableStudent other) {
        return ComparisonChain.start()
                .compare(name, other.name)
                .compare(age, other.age)
                .compare(score, other.score, Ordering.natural().nullsLast())
                .result();
    }


    public String getName() {
        return name;
    }

    @CvsSerialNum(value = 1)
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }
    @CvsSerialNum(value = 0)
    public void setAge(int age) {
        this.age = age;
    }

    public int getScore() {
        return score;
    }
    @CvsSerialNum(value = 2)
    public void setScore(int score) {
        this.score = score;
    }

    public ComparableStudent() {}
}

